package com.sojson.controller.po;

import java.io.IOException;
import java.sql.SQLException;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.sojson.config.exception.TransErrorCode;
import com.sojson.constant.Constant;
import com.sojson.controller.po.param.verify.HotelOrderParamVerify;
import com.sojson.controller.po.param.verify.OrderParamVerify;
import com.sojson.controller.po.param.verify.UserCouponParamVerify;
import com.sojson.entity.bo.CouponUseParam;
import com.sojson.entity.bo.OrderParam;
import com.sojson.entity.po.CouponGoods;
import com.sojson.entity.po.UserCoupon;
import com.sojson.entity.vo.UserCouponVo;
import com.sojson.result.Result;
import com.sojson.service.CouponGoodsService;
import com.sojson.service.UserCouponService;
import com.sojson.util.DateUtil;
import com.sojson.util.token.TokenUtil;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;

/**
 * 用户优惠券控制器
 * 
 * @author liu
 * @date 2021-06-02
 */
@Api(tags = "用户优惠券管理")
@RestController
@RequestMapping("/userCoupon")
public class UserCouponController {

    @Autowired
    private CouponGoodsService couponGoodsService;
    @Autowired
    private UserCouponService userCouponService;

    /**
     * 单条插入
     * 
     * @param userCoupon
     * @return
     */
    @PostMapping("/insert")
    @PreAuthorize("@ss.hasPermi('/userCoupon/insert')")
    @ApiOperation("给用户优惠券添加一条数据")
    public Result<Integer> insert(@RequestBody UserCoupon userCoupon) {
        return Result.success(userCouponService.insert(userCoupon));
    }

    /**
     * 批量插入
     * 
     * @param userCoupon
     * @return
     */
    @PostMapping("/insertList")
    @PreAuthorize("@ss.hasPermi('/userCoupon/insertList')")
    @ApiOperation("给用户优惠券添加多条数据")
    public Result<Integer> insertList(@RequestBody UserCoupon userCoupon) {
        return Result.success(userCouponService.insertList(userCoupon));
    }

    /**
     * 通过对象批量删除(物理删除)
     * 
     * @param userCoupon
     * @return
     */
    @DeleteMapping("/deleteByDto")
    @PreAuthorize("@ss.hasPermi('/userCoupon/deleteByDto')")
    @ApiOperation("通过对象批量删除用户优惠券数据(物理删除)")
    public Result<Integer> deleteByDto(@RequestBody UserCoupon userCoupon) {
        return Result.success(userCouponService.deleteByDto(userCoupon));
    }

    /**
     * 统计全部数量
     * 
     * @return
     */
    @GetMapping("/countAll")
    @PreAuthorize("@ss.hasPermi('/userCoupon/countAll')")
    @ApiOperation("统计用户优惠券全部数据(包含逻辑删除的)")
    public Result<Integer> countAll() {
        return Result.success(userCouponService.countAll());
    }

    /**
     * 查询所有
     * 
     * @return
     */
    @GetMapping("/listAll")
    @PreAuthorize("@ss.hasPermi('/userCoupon/listAll')")
    @ApiOperation("查询用户优惠券所有数据(包含逻辑删除的)")
    public Result<List<UserCoupon>> listAll() {
        return Result.success(userCouponService.listAll());
    }

    /**
     * 根据对象统计数量
     * 
     * @param userCoupon
     * @return
     */
    @GetMapping("/countByDto")
    @PreAuthorize("@ss.hasPermi('/userCoupon/countByDto')")
    @ApiOperation("根据对象统计用户优惠券数据")
    public Result<Integer> countByDto(UserCoupon userCoupon) {
        return Result.success(userCouponService.countByDto(userCoupon));
    }

    /**
     * 根据对象查询
     * 
     * @param userCoupon
     * @return
     */
    @GetMapping("/listByDto")
    @PreAuthorize("@ss.hasPermi('/userCoupon/listByDto')")
    @ApiOperation("根据对象查询用户优惠券数据")
    public Result<List<UserCoupon>> listByDto(UserCoupon userCoupon) {
        return Result.success(userCouponService.listByDto(userCoupon));
    }

    /**
     * 通过id删除(物理删除)
     * 
     * @param id
     * @return
     */
    @DeleteMapping("/deleteById/{id}")
    @PreAuthorize("@ss.hasPermi('/userCoupon/deleteById/{id}')")
    @ApiOperation("通过id删除用户优惠券数据(物理删除)")
    @ApiImplicitParam(name = "id", value = "id")
    public Result<Integer> deleteById(@PathVariable Integer id) {
        if (id == null) {
            return Result.success();
        }

        return Result.success(userCouponService.deleteById(id));
    }

    /**
     * 通过id数组删除(物理删除)
     * 
     * @param ids
     * @return
     */
    @DeleteMapping("/deleteByIds/{ids}")
    @PreAuthorize("@ss.hasPermi('/userCoupon/deleteByIds')")
    @ApiOperation("通过id数组删除用户优惠券数据(物理删除)")
    @ApiImplicitParam(name = "ids", value = "id数组")
    public Result<Integer> deleteByIds(@PathVariable Integer[] ids) {
        if (ids.length == 0) {
            return Result.success();
        }

        return Result.success(userCouponService.deleteByIds(ids));
    }

    /**
     * 通过id更新(全部更新)
     * 
     * @param userCoupon
     * @return
     */
    @PutMapping("/updateAllById")
    @PreAuthorize("@ss.hasPermi('/userCoupon/updateAllById')")
    @ApiOperation("通过id更新用户优惠券数据(全部更新)")
    public Result<Integer> updateAllById(@RequestBody UserCoupon userCoupon) {
        return Result.success(userCouponService.updateAllById(userCoupon));
    }

    /**
     * 通过id列表更新(全部更新)(更新内容相同)
     * 
     * @param userCoupon
     * @return
     */
    @PutMapping("/updateAllByIdList")
    @PreAuthorize("@ss.hasPermi('/userCoupon/updateAllByIdList')")
    @ApiOperation("通过id列表更新用户优惠券数据(全部更新)(更新内容相同)")
    public Result<Integer> updateAllByIdList(@RequestBody UserCoupon userCoupon) {
        return Result.success(userCouponService.updateAllByIdList(userCoupon));
    }

    /**
     * 通过id列表更新(全部更新)(更新内容不相同)
     * 
     * @param userCoupon
     * @return
     */
    @PutMapping("/updateDtoListAllById")
    @PreAuthorize("@ss.hasPermi('/userCoupon/updateDtoListAllById')")
    @ApiOperation("通过id列表更新用户优惠券数据(全部更新)(更新内容不相同)")
    public Result<Integer> updateDtoListAllById(@RequestBody UserCoupon userCoupon) {
        return Result.success(userCouponService.updateDtoListAllById(userCoupon));
    }

    /**
     * 通过id更新(局部更新)
     * 
     * @param userCoupon
     * @return
     */
    @PutMapping("/updateById")
    @PreAuthorize("@ss.hasPermi('/userCoupon/updateById')")
    @ApiOperation("通过id更新用户优惠券数据(局部更新)")
    public Result<Integer> updateById(@RequestBody UserCoupon userCoupon) {
        return Result.success(userCouponService.updateById(userCoupon));
    }

    /**
     * 通过id列表更新(局部更新)(更新内容相同)
     * 
     * @param userCoupon
     * @return
     */
    @PutMapping("/updateByIdList")
    @PreAuthorize("@ss.hasPermi('/userCoupon/updateByIdList')")
    @ApiOperation("通过id列表更新用户优惠券数据(局部更新)(更新内容相同)")
    public Result<Integer> updateByIdList(@RequestBody UserCoupon userCoupon) {
        return Result.success(userCouponService.updateByIdList(userCoupon));
    }

    /**
     * 通过id列表更新(局部更新)(更新内容不相同)
     * 
     * @param userCoupon
     * @return
     */
    @PutMapping("/updateDtoListById")
    @PreAuthorize("@ss.hasPermi('/userCoupon/updateDtoListById')")
    @ApiOperation("通过id列表更新用户优惠券数据(局部更新)(更新内容不相同)")
    public Result<Integer> updateDtoListById(@RequestBody UserCoupon userCoupon) {
        return Result.success(userCouponService.updateDtoListById(userCoupon));
    }

    /**
     * 通过id查询
     * 
     * @param id
     * @return
     */
    @GetMapping("/findById/{id}")
    @PreAuthorize("@ss.hasPermi('/userCoupon/findById/{id}')")
    @ApiOperation("通过id查询用户优惠券数据")
    @ApiImplicitParam(name = "id", value = "id")
    public Result<UserCoupon> findById(@PathVariable Integer id) {
        if (id == null) {
            return Result.success();
        }

        return Result.success(userCouponService.findById(id));
    }

    /**
     * 通过id数组查询
     * 
     * @param ids
     * @return
     */
    @GetMapping("/findByIds/{ids}")
    @PreAuthorize("@ss.hasPermi('/userCoupon/findByIds/{ids}')")
    @ApiOperation("通过id数组查询用户优惠券数据")
    @ApiImplicitParam(name = "ids", value = "id数组")
    public Result<List<UserCoupon>> findByIds(@PathVariable Integer[] ids) {
        if (ids.length == 0) {
            return Result.success();
        }

        return Result.success(userCouponService.findByIds(ids));
    }

    /**
     * 通过对象查询id列表
     * 
     * @param userCoupon
     * @return
     */
    @GetMapping("/findIdByDto")
    @PreAuthorize("@ss.hasPermi('/userCoupon/findIdByDto')")
    @ApiOperation("通过对象查询用户优惠券id列表")
    public Result<List<Integer>> findIdByDto(UserCoupon userCoupon) {
        return Result.success(userCouponService.findIdByDto(userCoupon));
    }

    /**
     * 通过no删除(物理删除)
     * 
     * @param no
     * @return
     */
    @DeleteMapping("/deleteByNo/{no}")
    @PreAuthorize("@ss.hasPermi('/userCoupon/deleteByNo/{no}')")
    @ApiOperation("通过no删除用户优惠券数据(物理删除)")
    @ApiImplicitParam(name = "no", value = "no")
    public Result<Integer> deleteByNo(@PathVariable String no) {
        if (no == null) {
            return Result.success();
        }

        return Result.success(userCouponService.deleteByNo(no));
    }

    /**
     * 通过no数组删除(物理删除)
     * 
     * @param nos
     * @return
     */
    @DeleteMapping("/deleteByNos/{nos}")
    @PreAuthorize("@ss.hasPermi('/userCoupon/deleteByNos/{nos}')")
    @ApiOperation("通过no数组删除用户优惠券数据(物理删除)")
    @ApiImplicitParam(name = "nos", value = "no数组")
    public Result<Integer> deleteByNos(@PathVariable String[] nos) {
        if (nos.length == 0) {
            return Result.success();
        }

        return Result.success(userCouponService.deleteByNos(nos));
    }

    /**
     * 通过no更新(全部更新)
     * 
     * @param userCoupon
     * @return
     */
    @PutMapping("/updateAllByNo")
    @PreAuthorize("@ss.hasPermi('/userCoupon/updateAllByNo')")
    @ApiOperation("通过no更新用户优惠券数据(全部更新)")
    public Result<Integer> updateAllByNo(@RequestBody UserCoupon userCoupon) {
        return Result.success(userCouponService.updateAllByNo(userCoupon));
    }

    /**
     * 通过no列表更新(全部更新)(更新内容相同)
     * 
     * @param userCoupon
     * @return
     */
    @PutMapping("/updateAllByNoList")
    @PreAuthorize("@ss.hasPermi('/userCoupon/updateAllByNoList')")
    @ApiOperation("通过no列表更新用户优惠券数据(全部更新)(更新内容相同)")
    public Result<Integer> updateAllByNoList(@RequestBody UserCoupon userCoupon) {
        return Result.success(userCouponService.updateAllByNoList(userCoupon));
    }

    /**
     * 通过no列表更新(全部更新)(更新内容不相同)
     * 
     * @param userCoupons
     * @return
     */
    @PutMapping("/updateDtoListAllByNo")
    @PreAuthorize("@ss.hasPermi('/userCoupon/updateDtoListAllByNo')")
    @ApiOperation("通过no列表更新用户优惠券数据(全部更新)(更新内容不相同)")
    public Result<Integer> updateDtoListAllByNo(@RequestBody UserCoupon userCoupons) {
        return Result.success(userCouponService.updateDtoListAllByNo(userCoupons));
    }

    /**
     * 通过no更新(局部更新)
     * 
     * @param userCoupon
     * @return
     */
    @PutMapping("/updateByNo")
    @PreAuthorize("@ss.hasPermi('/userCoupon/updateByNo')")
    @ApiOperation("通过no更新用户优惠券数据(局部更新)")
    public Result<Integer> updateByNo(@RequestBody UserCoupon userCoupon) {
        return Result.success(userCouponService.updateByNo(userCoupon));
    }

    /**
     * 通过no列表更新(局部更新)(更新内容相同)
     * 
     * @param userCoupon
     * @return
     */
    @PutMapping("/updateByNoList")
    @PreAuthorize("@ss.hasPermi('/userCoupon/updateByNoList')")
    @ApiOperation("通过no列表更新用户优惠券数据(局部更新)(更新内容相同)")
    public Result<Integer> updateByNoList(@RequestBody UserCoupon userCoupon) {
        return Result.success(userCouponService.updateByNoList(userCoupon));
    }

    /**
     * 通过no列表更新(局部更新)(更新内容不相同)
     * 
     * @param userCoupons
     * @return
     */
    @PutMapping("/updateDtoListByNo")
    @PreAuthorize("@ss.hasPermi('/userCoupon/updateDtoListByNo')")
    @ApiOperation("通过no列表更新用户优惠券数据(局部更新)(更新内容不相同)")
    public Result<Integer> updateDtoListByNo(@RequestBody UserCoupon userCoupons) {
        return Result.success(userCouponService.updateDtoListByNo(userCoupons));
    }

    /**
     * 通过no查询
     * 
     * @param no
     * @return
     */
    @GetMapping("/findByNo/{no}")
    @PreAuthorize("@ss.hasPermi('/userCoupon/findByNo/{no}')")
    @ApiOperation("通过no查询用户优惠券数据")
    @ApiImplicitParam(name = "no", value = "no")
    public Result<UserCoupon> findByNo(@PathVariable String no) {
        if (no == null) {
            return Result.success();
        }

        return Result.success(userCouponService.findByNo(no));
    }

    /**
     * 通过no数组查询
     * 
     * @param nos
     * @return
     */
    @GetMapping("/findByNos/{nos}")
    @PreAuthorize("@ss.hasPermi('/userCoupon/findByNos/{nos}')")
    @ApiOperation("通过no数组查询用户优惠券数据")
    @ApiImplicitParam(name = "nos", value = "no数组")
    public Result<List<UserCoupon>> findByNos(@PathVariable String[] nos) {
        if (nos.length == 0) {
            return Result.success();
        }

        return Result.success(userCouponService.findByNos(nos));
    }

    /**
     * 通过对象查询no列表
     * 
     * @param userCoupon
     * @return
     */
    @GetMapping("/findNoByDto")
    @PreAuthorize("@ss.hasPermi('/userCoupon/findNoByDto')")
    @ApiOperation("通过对象查询用户优惠券no列表")
    public Result<List<String>> findNoByDto(UserCoupon userCoupon) {
        return Result.success(userCouponService.findNoByDto(userCoupon));
    }

    /**
     * 清空数据(物理清空)
     * 
     * @return
     */
    @DeleteMapping("/clear")
    @PreAuthorize("@ss.hasPermi('/userCoupon/clear')")
    @ApiOperation("清空用户优惠券数据(物理清空)")
    public Result<Integer> clear() {
        return Result.success(userCouponService.clear());
    }

    /**
     * 领取优惠券
     * 
     * @param activityId
     * @return
     * @throws Exception 
     */
    @PostMapping(value = "/addCoupon/{couponId}")
    @PreAuthorize("@ss.login()")
    public Object addCoupon(@PathVariable Integer couponId) throws Exception {
        // 校验参数
        Result<?> paramResult = UserCouponParamVerify.addCoupon(couponId);
        // 验证不通过
        if (!paramResult.getCode().equals(TransErrorCode.SUCCESS.getCode())) {
            return paramResult;
        }

        return Result.success(userCouponService.addCoupon(couponId));
    }

    /**
     * 活动领取优惠券
     * 
     * @param activityId
     * @return
     * @throws Exception 
     */
    @PostMapping(value = "/addCouponByActivity/{activityId}")
    @PreAuthorize("@ss.login()")
    public Object addCouponByActivity(@PathVariable Integer activityId) throws Exception {
        // 校验参数
        Result<?> paramResult = UserCouponParamVerify.addCouponByActivity(activityId);
        // 验证不通过
        if (!paramResult.getCode().equals(TransErrorCode.SUCCESS.getCode())) {
            return paramResult;
        }

        return Result.success(userCouponService.addCouponByActivity(activityId));
    }

    /**
     * 获取优惠券(简单的参数)
     * 
     * @param userCoupon
     * @return
     * @throws Exception 
     */
    @GetMapping(value = "/listSimple")
    @PreAuthorize("@ss.hasPermi('/userCoupon/listSimple')")
    public Result<List<UserCouponVo>> listSimple(UserCouponVo userCoupon) throws Exception {
        List<UserCouponVo> listSimple = userCouponService.listSimple(userCoupon);
        return Result.success(listSimple, listSimple);
    }

    /**
     * 获取优惠券(简单的参数)
     * 
     * @param userCoupon
     * @return
     * @throws Exception 
     */
    @GetMapping(value = "/listSimpleApp")
    @PreAuthorize("@ss.login()")
    public Result<List<UserCouponVo>> listSimpleApp(UserCouponVo userCoupon) throws Exception {
        userCoupon.setUserNo(TokenUtil.getUserNo());
        userCoupon.setThisTime(DateUtil.nowDate());
        userCoupon.setAbortTime(Constant.COUPON_SELECT_ABORT_TIME_DAY);

        List<UserCouponVo> listSimple = userCouponService.listSimple(userCoupon);
        return Result.success(listSimple, listSimple);
    }

    /**
     * 获取优惠券(指定某些商品可用)
     * 
     * @param couponUse
     * @return
     * @throws Exception 
     */
    @PostMapping(value = "/list")
    @PreAuthorize("@ss.hasPermi('/userCoupon/list')")
    public Result<List<UserCouponVo>> list(@RequestBody CouponUseParam couponUse) throws Exception {
        couponUse.init();

        List<UserCouponVo> list = userCouponService.list(couponUse);
        return Result.success(list, list);
    }

    /**
     * 获取优惠券(指定某些商品可用)
     * 
     * @param couponUse
     * @return
     * @throws Exception 
     */
    @PostMapping(value = "/listApp")
    @PreAuthorize("@ss.login()")
    public Result<List<UserCouponVo>> listApp(@RequestBody CouponUseParam couponUse) throws Exception {
        couponUse.initApp();

        List<UserCouponVo> list = userCouponService.list(couponUse);
        return Result.success(list, list);
    }

    /**
     * 根据对象统计数量
     * 
     * @param couponUse
     * @return
     */
    @PostMapping("/countListByDto")
    @PreAuthorize("@ss.hasPermi('/userCoupon/countListByDto')")
    @ApiOperation("根据对象统计用户优惠券数据")
    public Result<Integer> countListByDto(@RequestBody CouponUseParam couponUse) {
        couponUse.init();

        int count = userCouponService.countListByDto(couponUse);
        return Result.success(count);
    }

    /**
     * 根据对象统计数量
     * 
     * @param couponUse
     * @return
     * @throws IOException 
     */
    @PostMapping("/countListByDtoApp")
    @PreAuthorize("@ss.login()")
    @ApiOperation("根据对象统计用户优惠券数据")
    public Result<Integer> countListByDtoApp(@RequestBody CouponUseParam couponUse) throws IOException {
        couponUse.initApp();

        Integer count = userCouponService.countListByDto(couponUse);
        return Result.success(count);
    }

    /**
     * 获取不可用优惠券
     * 
     * @param couponUse
     * @return
     * @throws Exception 
     */
    @PostMapping(value = "/listNo")
    @PreAuthorize("@ss.hasPermi('/userCoupon/listNo')")
    public Object listNo(@RequestBody CouponUseParam couponUse) throws Exception {
        couponUse.init();

        List<UserCouponVo> listNo = userCouponService.listNo(couponUse);
        return Result.success(listNo, listNo);
    }

    /**
     * 获取不可用优惠券
     * 
     * @param userCoupon
     * @return
     * @throws Exception 
     */
    @PostMapping(value = "/listNoApp")
    @PreAuthorize("@ss.login()")
    public Object listNoApp(@RequestBody CouponUseParam couponUse) throws Exception {
        couponUse.initApp();

        List<UserCouponVo> listNo = userCouponService.listNo(couponUse);
        return Result.success(listNo, listNo);
    }

    /**
     * 修改优惠券状态
     * 
     * @param userCoupon
     * @return
     * @throws SQLException
     */
    @PutMapping("/updateStatus")
    @PreAuthorize("@ss.hasPermi('/userCoupon/updateStatus')")
    public Object updateStatus(@RequestBody UserCoupon userCoupon) throws SQLException {
        // 校验参数
        Result<?> paramResult = UserCouponParamVerify.updateStatus(userCoupon);
        // 验证不通过
        if (!paramResult.getCode().equals(TransErrorCode.SUCCESS.getCode())) {
            return paramResult;
        }

        UserCoupon cou = new UserCoupon();
        cou.updateInit();
        cou.setId(userCoupon.getId());
        cou.setStatus(userCoupon.getStatus());
        userCouponService.updateStatus(userCoupon);
        return Result.success();
    }

    /**
     * 判断优惠券是否可以在此商品上使用
     * 
     * @param couponGoods
     * @return
     */
    @GetMapping("/isUse")
    @PreAuthorize("@ss.login()")
    public Object isUse(CouponGoods couponGoods) {
        // 校验参数
        Result<?> paramResult = UserCouponParamVerify.isUse(couponGoods);
        // 验证不通过
        if (!paramResult.getCode().equals(TransErrorCode.SUCCESS.getCode())) {
            return paramResult;
        }

        CouponGoods cou = new CouponGoods();
        cou.setCouponNo(couponGoods.getCouponNo());
        cou.setGoodsNo(couponGoods.getGoodsNo());
        return Result.success(couponGoodsService.countByDto(cou));
    }

    /**
     * 获取商城优惠金额
     * 
     * @param orderParam
     * @return
     * @throws Exception
     */
    @PreAuthorize("@ss.login()")
    @PostMapping(value = "/getPriceGoods")
    public Object getPriceGoods(@RequestBody OrderParam orderParam) throws Exception {
        // 校验参数
        Result<?> paramResult = OrderParamVerify.ordersBuy(orderParam);
        // 验证不通过
        if (!paramResult.getCode().equals(TransErrorCode.SUCCESS.getCode())) {
            return paramResult;
        }

        return Result.success(couponGoodsService.getPriceGoods(orderParam));
    }

    /**
     * 获取酒店优惠金额
     * 
     * @param orderParam
     * @return
     * @throws Exception
     */
    @PreAuthorize("@ss.login()")
    @PostMapping(value = "/getPriceHotel")
    public Object getPriceHotel(@RequestBody OrderParam orderParam) throws Exception {
        // 校验参数
        Result<?> paramResult = HotelOrderParamVerify.orders(orderParam);
        // 验证不通过
        if (!paramResult.getCode().equals(TransErrorCode.SUCCESS.getCode())) {
            return paramResult;
        }

        return Result.success(couponGoodsService.getPriceHotel(orderParam));
    }

}